package com.irdstudio.bsp.console.service.impl;

import java.util.List;
import java.util.Objects;

import com.irdstudio.bsp.console.dao.PluginApproveConfDao;
import com.irdstudio.bsp.console.dao.PluginAssignConfDao;
import com.irdstudio.bsp.console.dao.PluginContentionConfDao;
import com.irdstudio.bsp.console.dao.PluginCountersignConfDao;
import com.irdstudio.bsp.console.dao.domain.PluginApproveConf;
import com.irdstudio.bsp.console.dao.domain.PluginAssignConf;
import com.irdstudio.bsp.console.dao.domain.PluginContentionConf;
import com.irdstudio.bsp.console.dao.domain.PluginCountersignConf;
import com.irdstudio.bsp.console.service.facade.BpmNodeInfoService;
import com.irdstudio.bsp.console.service.vo.BpmNodeInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.bsp.console.dao.BpmNodeInfoDao;
import com.irdstudio.bsp.console.dao.domain.BpmNodeInfo;
import com.irdstudio.sdk.beans.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 流程节点信息				<p>
 * @author srvadmin
 * @date 2020-07-24
 */
@Service("bpmNodeInfoServiceImpl")
public class BpmNodeInfoServiceImpl implements BpmNodeInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BpmNodeInfoServiceImpl.class);

	@Autowired
	private BpmNodeInfoDao bpmNodeInfoDao;

	@Autowired
	private PluginApproveConfDao pluginApproveConfDao;

	@Autowired
	private PluginCountersignConfDao pluginCountersignConfDao;

	@Autowired
	private PluginAssignConfDao pluginAssignConfDao;

	@Autowired
	private PluginContentionConfDao pluginContentionConfDao;

	
	/**
	 * 新增操作
	 */
	public int insertBpmNodeInfo(BpmNodeInfoVO inBpmNodeInfoVo) {
		logger.debug("当前新增数据为:"+ inBpmNodeInfoVo.toString());
		int num = 0;
		try {
			BpmNodeInfo bpmNodeInfo = new BpmNodeInfo();
			beanCopy(inBpmNodeInfoVo, bpmNodeInfo);
			num = bpmNodeInfoDao.insertBpmNodeInfo(bpmNodeInfo);
			if (num == 1) {
				BpmNodeTypeEnum type = BpmNodeTypeEnum.get(inBpmNodeInfoVo.getBpmNodeType());
				if (type != null) {
					switch (type) {
						case PluginApproveConf :
							PluginApproveConf pluginApproveConf = new PluginApproveConf();
							pluginApproveConf.setPluginConfId(inBpmNodeInfoVo.getBpmNodeId());
							pluginApproveConfDao.insertPluginApproveConf(pluginApproveConf);
							break;
						case PluginCountersignConf:
							PluginCountersignConf pluginCountersignConf = new PluginCountersignConf();
							pluginCountersignConf.setPluginConfId(inBpmNodeInfoVo.getBpmNodeId());
							pluginCountersignConfDao.insertPluginCountersignConf(pluginCountersignConf);
							break;
						case PluginContentionConf:
							PluginContentionConf pluginContentionConf = new PluginContentionConf();
							pluginContentionConf.setPluginConfId(inBpmNodeInfoVo.getBpmNodeId());
							pluginContentionConfDao.insertPluginContentionConf(pluginContentionConf);
							break;
						case PluginAssignConf:
							PluginAssignConf pluginAssignConf = new PluginAssignConf();
							pluginAssignConf.setPluginConfId(inBpmNodeInfoVo.getBpmNodeId());
							pluginAssignConfDao.insertPluginAssignConf(pluginAssignConf);
							break;
						default:
							break;
					}
				}

			}
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BpmNodeInfoVO inBpmNodeInfoVo) {
		logger.debug("当前删除数据条件为:"+ inBpmNodeInfoVo);
		int num = 0;
		try {
			BpmNodeInfo bpmNodeInfo = new BpmNodeInfo();
			beanCopy(inBpmNodeInfoVo, bpmNodeInfo);
			num = bpmNodeInfoDao.deleteByPk(bpmNodeInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBpmNodeInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BpmNodeInfoVO inBpmNodeInfoVo) {
		logger.debug("当前修改数据为:"+ inBpmNodeInfoVo.toString());
		int num = 0;
		try {
			BpmNodeInfoVO checkExistsObj = this.queryByPk(inBpmNodeInfoVo);
			if (checkExistsObj == null) {
				num = this.insertBpmNodeInfo(inBpmNodeInfoVo);
			} else {
				BpmNodeInfo bpmNodeInfo = new BpmNodeInfo();
				beanCopy(inBpmNodeInfoVo, bpmNodeInfo);
				num = bpmNodeInfoDao.updateByPk(bpmNodeInfo);
			}
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBpmNodeInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BpmNodeInfoVO queryByPk(BpmNodeInfoVO inBpmNodeInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inBpmNodeInfoVo);
		try {
			BpmNodeInfo queryBpmNodeInfo = new BpmNodeInfo();
			beanCopy(inBpmNodeInfoVo, queryBpmNodeInfo);
			BpmNodeInfo queryRslBpmNodeInfo = bpmNodeInfoDao.queryByPk(queryBpmNodeInfo);
			if (Objects.nonNull(queryRslBpmNodeInfo)) {
				BpmNodeInfoVO outBpmNodeInfoVo = beanCopy(queryRslBpmNodeInfo, new BpmNodeInfoVO());
				logger.debug("当前查询结果为:"+ outBpmNodeInfoVo.toString());
				return outBpmNodeInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BpmNodeInfoVO> queryAllOwner(BpmNodeInfoVO bpmNodeInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BpmNodeInfoVO> list = null;
		try {
			List<BpmNodeInfo> bpmNodeInfos = bpmNodeInfoDao.queryAllOwnerByPage(bpmNodeInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bpmNodeInfos.size());
			pageSet(bpmNodeInfos, bpmNodeInfoVo);
			list = (List<BpmNodeInfoVO>) beansCopy(bpmNodeInfos, BpmNodeInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BpmNodeInfoVO> queryAllCurrOrg(BpmNodeInfoVO bpmNodeInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BpmNodeInfo> bpmNodeInfos = bpmNodeInfoDao.queryAllCurrOrgByPage(bpmNodeInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bpmNodeInfos.size());
		List<BpmNodeInfoVO> list = null;
		try {
			pageSet(bpmNodeInfos, bpmNodeInfoVo);
			list = (List<BpmNodeInfoVO>) beansCopy(bpmNodeInfos, BpmNodeInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BpmNodeInfoVO> queryAllCurrDownOrg(BpmNodeInfoVO bpmNodeInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BpmNodeInfo> bpmNodeInfos = bpmNodeInfoDao.queryAllCurrDownOrgByPage(bpmNodeInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bpmNodeInfos.size());
		List<BpmNodeInfoVO> list = null;
		try {
			pageSet(bpmNodeInfos, bpmNodeInfoVo);
			list = (List<BpmNodeInfoVO>) beansCopy(bpmNodeInfos, BpmNodeInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
}
